//
//  LeftViewController.m
//  Delicious
//
//  Created by gy on 16/5/5.
//  Copyright © 2016年 gy. All rights reserved.
//

#import "LeftViewController.h"
@interface LeftViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *arr;
@property (nonatomic, strong) NSArray *VCNames; //控制器的名字
@property (nonatomic, assign) NSInteger currentIndex; //当前显示的控制器的下标
@end

@implementation LeftViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _currentIndex = 0;
    // Do any additional setup after loading the view.
    UIImageView *backImageView = [[UIImageView alloc]initWithFrame:self.view.frame];
    backImageView.image = [UIImage imageNamed:@"1.png"];
    [self.view addSubview:backImageView];
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    self.tableView.backgroundColor = [UIColor clearColor];
    // 表头
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 150)];
    headView.backgroundColor = [UIColor clearColor];
    self.tableView.tableHeaderView = headView;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.arr = @[@"视频", @"养生", @"运动",@"设置"];
    self.VCNames = @[@"VideoViewController",@"HealthViewController", @"SportsViewController",@"SetViewController"];
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    cell.textLabel.text = self.arr[indexPath.row];
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.textColor = [UIColor colorWithRed:0.234 green:1.000 blue:0.635 alpha:1.000];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    drawerViewController *drawer = (drawerViewController *)delegate.window.rootViewController;

        UINavigationController *nagVC = [delegate.VCDic  objectForKey:self.VCNames[indexPath.row]];
        if (nagVC == nil)
        {
            Class class = NSClassFromString(self.VCNames[indexPath.row]);
            UIViewController *VC = [[class alloc]init];
            nagVC = [[UINavigationController alloc]initWithRootViewController:VC];
            UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(leftClick)];
            VC.navigationItem.leftBarButtonItem = leftItem;
            [delegate.VCDic setValue:nagVC forKey:self.VCNames[indexPath.row]];
        }
        [drawer setNewMainVC:nagVC];
        self.currentIndex = indexPath.row;
    
    [drawer close];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)leftClick{
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    drawerViewController *drawer = (drawerViewController *)delegate.window.rootViewController;
    UINavigationController *nagvc = [delegate.VCDic objectForKey:@"tabBarController"];
    [drawer setNewMainVC:nagvc];
   
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
